#include <buola/scene/cperspectivecamera.h>

namespace buola { namespace scene {

CPerspectiveCamera::CPerspectiveCamera()
    :   mFOV(M_PI/6)
    ,   mFOVMode(FOV_VERT)
    ,   mNear(1.0)
    ,   mFar(1000.0)
    ,   mShift(0,0)
{
}

void CPerspectiveCamera::SetFOV(double pFOV,EFOVMode pMode)
{
    mFOV=pFOV;
    mFOVMode=pMode;
    mProjectionValid=false;
}

void CPerspectiveCamera::SetClipping(double pNear,double pFar)
{
    mNear=pNear;
    mFar=pFar;
    mProjectionValid=false;
}

void CPerspectiveCamera::SetShift(const CPoint_d &pPos,const CSize_d &pSize)
{
    mShift={pPos.x/pSize.x,pPos.y/pSize.y};
}

void CPerspectiveCamera::CalcProjection()
{
    double lFOV;

    switch(mFOVMode)
    {
    case FOV_VERT:
    default:
        lFOV=mFOV;
        break;
    case FOV_HORZ:
        lFOV=mFOV/mAspect;
        break;
    case FOV_MIN:
        if(mAspect>1.0)
            lFOV=mFOV;
        else
            lFOV=mFOV/mAspect;
        break;
    case FOV_MAX:
        if(mAspect<1.0)
            lFOV=mFOV;
        else
            lFOV=mFOV/mAspect;
        break;
    }

    mProjection=mat::perspective(lFOV,mAspect,mNear,mFar);
    mat::shift_perspective_center(mProjection,mShift);
    mProjectionValid=true;
}

/*namespace scene*/ } /*namespace buola*/ }
